﻿#include "dingdanpicker.h"
#include "ui_dingdanpicker.h"

DingdanPicker::DingdanPicker(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DingdanPicker)
{
    ui->setupUi(this);

    setWindowTitle("选择单据");

    ui->tableView->setModel(&model);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    connect(&dingdanService, &DingdanService::getDingdansSuccess,
            [=](QList<Dingdan*> dingdans)
    {
        model.reset();
        model.insertDingdan(dingdans, 0);
        this->exec();
    });

    connect(ui->tableView, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        Q_UNUSED(index);
        Dingdan *dingdan = model.getDingdan(ui->tableView->currentIndex().row());
        this->close();
        emit this->selectedFinished(dingdan);
    });
}

DingdanPicker::~DingdanPicker()
{
    delete ui;
}

void DingdanPicker::setDingdanType(Dingdan::DingdanType type){
    this->type = type;
    model.setWidgetType(type);
}

void DingdanPicker::setDingdansState(Dingdan::DingdanState state){
    this->state = state;
}

void DingdanPicker::showAndInit(){
    dingdanService.getDingdans(this, type, state);
}
